import Vue from 'vue'
import { Message } from 'element-ui';
Vue.directive('copy', {
  inserted(el, binding) {
    const { value } = binding
    el.$value = value
    el.onclick = () => {
      if (!el.$value) {
        Message.warning('暂无复制内容')
        return
      }
      const textarea = document.createElement('textarea')
      textarea.readOnly = 'readonly'
      textarea.style.position = 'absolute'
      textarea.style.left = '-9999px'
      textarea.value = el.$value
      document.body.appendChild(textarea)
      textarea.select()
      const result = document.execCommand('Copy')
      console.log(result)
      if (result) {
        Message.success('复制成功')
      }
      document.body.removeChild(textarea)
      el.addEventListener('click', el.handler)
    }
  },
  update(el, binding) {
    const { value } = binding
    el.$value = value
  }
})